const fs = require("fs");
const xlsx = require("node-xlsx");

const sortData = xlsx.parse("./excelFiles/sortData.xlsx");

const addressData = xlsx.parse("./excelFiles/address.xlsx");

// 去掉错误数据
const filter = (originalList, addressList) => {
  const addressMap = {};
  addressList.forEach((address) => {
    if (address[2] && address[2] !== "null") {
      addressMap[address[0]] = address[2];
    }
  });
  const data = originalList.map((item, idx) => {
    const address = addressMap[item[0]];
    if (address) {
      return [...item, address];
    }
    return [...item];
  });
  // console.log(data);
  return [
    {
      name: "sheet1",
      data,
    },
  ];
};

filter(sortData[0].data, addressData[0].data);

// 生成表格数据对象  wch为设置列宽度
const buffer = xlsx.build(filter(sortData[0].data, addressData[0].data), {
  sheetOptions: { "!cols": [{ wch: 10 }, { wch: 10 }] },
});

//生成excel
fs.writeFile("./jsonFiles/resultData.xlsx", buffer, (err) => {
  if (err) throw err;
  console.log("生成完毕");
});
